package com.dw.dwdzcommerce.base.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.ShareCompat;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by zz on 15/11/17. 抽象出基础功能的Fragment
 */
public abstract class BaseFragment extends Fragment {

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		int viewLayout = getViewLayout();
		View view = inflater.inflate(viewLayout, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		initView(view);
		bindView(view);
		onViewGreate(view, savedInstanceState);
	}

	public abstract int getViewLayout();

	/**
	 * onViewCreated后首先调用
	 * 
	 * @param view
	 */
	protected abstract void initView(View view);

	/**
	 * 在initView后立即调用
	 * 
	 * @param view
	 */
	protected abstract void bindView(View view);

	/**
	 * 该方法在bindView后被立即调用
	 *
	 * @param view
	 * @param savedInstanceState
	 */
	public abstract void onViewGreate(View view, @Nullable Bundle savedInstanceState);

	public void intentTo(Class<?> clazz, Bundle bundle) {
		Intent intent = getIntent(clazz);
		if (bundle != null)
			intent.putExtras(bundle);
		getActivity().startActivity(intent);
	}

	public Intent getIntent(Class<?> clazz) {
		Intent intent = ShareCompat.IntentBuilder.from(getActivity()).getIntent();
		intent.setClass(getActivity(), clazz);
		return intent;
	}

	public void launch(Class<?> clazz, View transitionView, String url, String shareName) {
		Intent intent = getIntent(clazz);
		intent.putExtra("url", url);
		// 这里指定了共享的视图元素
		ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),
				transitionView, shareName);
		ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
	}

	/**
	 * 调用该方法可以给EditText设置一个软键盘done按下时回调的事件,重写onEditDone以处理回调事件
	 *
	 * @param edittext
	 */
	public void setOnEditDone(EditText edittext) {
		edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					hideSoftInputFromWindow(v);
					onEditDone();
					return true;
				}
				return false;
			}
		});
	}

	/**
	 * 重写该方法以完成当软键盘done被按下时的调用,为使该方法见效,必须调用 setOnEditDone方法
	 */
	protected void onEditDone() {

	}

	public void hideSoftInputFromWindow(View view) {
		/* 隐藏软键盘 */
		InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager.isActive()) {
			inputMethodManager.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
		}
	}

}
